package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.StartupPageBo;
import com.joker.orange.domain.vo.StartupPageVo;
import com.joker.orange.domain.StartupPage;
import com.joker.orange.mapper.StartupPageMapper;
import com.joker.orange.service.IStartupPageService;

import java.util.*;

/**
 * 启动页信息Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class StartupPageServiceImpl implements IStartupPageService {

    private final StartupPageMapper baseMapper;

    /**
     * 查询启动页信息
     */
    @Override
    public StartupPageVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询启动页信息列表
     */
    @Override
    public TableDataInfo<StartupPageVo> queryPageList(StartupPageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StartupPage> lqw = buildQueryWrapper(bo);
        Page<StartupPageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询启动页信息列表
     */
    @Override
    public List<StartupPageVo> queryList(StartupPageBo bo) {
        LambdaQueryWrapper<StartupPage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StartupPage> buildQueryWrapper(StartupPageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StartupPage> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getHyperlinkAddress()), StartupPage::getHyperlinkAddress, bo.getHyperlinkAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDisplay()), StartupPage::getIsDisplay, bo.getIsDisplay());
        lqw.orderByAsc(StartupPage::getDisplayOrder);
        return lqw;
    }

    /**
     * 新增启动页信息
     */
    @Override
    public Boolean insertByBo(StartupPageBo bo) {
        StartupPage add = BeanUtil.toBean(bo, StartupPage.class);
        validEntityBeforeSave(add, bo.getImageUrlCopy());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改启动页信息
     */
    @Override
    public Boolean updateByBo(StartupPageBo bo) {
        StartupPage update = BeanUtil.toBean(bo, StartupPage.class);
        validEntityBeforeSave(update, bo.getImageUrlCopy());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StartupPage entity, String imageUrlCopy){
        entity.setImageUrl(imageUrlCopy);
    }

    /**
     * 批量删除启动页信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
